﻿using System;
using System.Collections.Generic;
using System.Text;
using OhioState.Graphics.Proxy;
using OhioState.Graphics;
using System.IO;

namespace OhioState.Graphics.Proxy
{
    public class VectorField3D : Volume//Texture3D
    {
        private int xDim, yDim, zDim;
        private String fileName;
        //private bool isRaw;

        /// <summary>
        /// File Extension
        /// </summary>
        //protected String extension;
        private static TexelFormat defaultTexelFormat = new TexelFormat();

        /// <summary>
        /// VectorField3D Constructor
        /// </summary>
        /// <param name="filename">filename</param>
        /// <param name="xDim">x Dim</param>
        /// <param name="yDim">y Dim</param>
        /// <param name="zDim">z Dim</param>
        /// <param name="format">Texel Format</param>
        public VectorField3D(String filename, int xDim, int yDim, int zDim, TexelFormat format)
            : base("temp", xDim, yDim, zDim, format)
        {
            this.fileName = filename;
            this.extension = System.IO.Path.GetExtension(filename);
            this.Name = Path.GetFileName(filename);

            ReadFile();

            //this.isRaw = false;
            //if (extension == ".raw")
            //    this.isRaw = true;
            this.xDim = xDim;
            this.yDim = yDim;
            this.zDim = zDim;
            TexelFormat texFormat = format;
            texFormat.Type = ByteFormat.BYTE;
            texFormat.Format = ImageFormat.RGBA;//ALPHA;
            //if (extension == ".dat")
            //{
            //    texFormat.Type = ByteFormat.SHORT;
            //    //texFormat.Format = ImageFormat.RGBA;
            //}

            this.TexelType = texFormat;

            //ReadHeader();

            this._width = this.xDim;
            this._height = this.yDim;
            this._depth = this.zDim;
        }

        float[, ,] vX = new float[48, 48, 48];
        float[, ,] vY = new float[48, 48, 48];
        float[, ,] vZ = new float[48, 48, 48];
        float max = -100.0f;
        float min = 100.0f;

        private void ReadFile()
        {
            FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            BinaryReader binaryReader = new BinaryReader(fileStream);

            int x = binaryReader.ReadInt32();
            int y = binaryReader.ReadInt32();
            int z = binaryReader.ReadInt32();

            //int next = binaryReader.ReadInt32();
            //float next2 = binaryReader.ReadSingle();
            //uint next3 = binaryReader.ReadUInt32();
            for (int i = 0; i < 48; i++)
            {
                for (int j = 0; j < 48; j++)
                {
                    for (int k = 0; k < 48; k++)
                    {
                        float vx = 0, vy = 0, vz = 0;
                        //if (i < 48 && j < 48 && k < 48)
                        {
                            vx = binaryReader.ReadSingle();
                            vy = binaryReader.ReadSingle();
                            vz = binaryReader.ReadSingle();
                        }
                        vX[k, j, i] = vy;//vx;
                        vY[k, j, i] = vx;// vy;
                        vZ[k, j, i] = vz;
                        if (min > vx) min = vx;
                        if (min > vy) min = vy;
                        if (min > vz) min = vz;
                        if (max < vx) max = vx;
                        if (max < vy) max = vy;
                        if (max < vz) max = vz;
                    }
                }
            }
        }

        public override byte[] GetTextureByte(out int xDim, out int yDim, out int zDim)
        {
            xDim = 48;//64;
            yDim = 48;//64;
            zDim = 48;//64;

            byte[] texels = new byte[xDim * yDim * zDim * 4];
            int index = 0;

            min = min >= 0 ? 0 : min;
            for (int k = 0; k < zDim; k++)
            {
                for (int j = 0; j < yDim; j++)
                {
                    for (int i = 0; i < xDim; i++)
                    {
                        float vx = vX[i, j, k] - min;//making sure it's always >= 0
                        float vy = vY[i, j, k] - min;
                        float vz = vZ[i, j, k] - min;

                        index = i * 4 + j * 48 * 4 + k * 48 * 48 * 4;
                        texels[index] = (byte)(vx * 255.0f);
                        texels[index + 1] = (byte)(vy * 255.0f);
                        texels[index + 2] = (byte)(vz * 255.0f);
                        texels[index + 3] = (byte)255;
                        //index += 4;
                    }
                }
            }

            return texels;
        }
    }
}
